[SAP ABAP开发技术总结]FTP到文件服务器,服务器上文件读写
20.3. FTP
CONSTANTS: c_key TYPE i VALUE 26101957."密钥
CONSTANTS: c_pwd(10) VALUE 'suning@123'.
CONSTANTS: c_user(6) VALUE 'sh_set'.
CONSTANTS: c_host(14) VALUE '192.168.118.81'.
**SAPFTPA:表示以SAP服务器为目的地,上传与下载都会放在SAP服务器上;SAPFTP:以Client端为目的地
CONSTANTS: c_rfcdest LIKE rfcdes-rfcdest VALUE 'SAPFTPA'.
DATA:lv_pwd(40).
DATA:lv_command(99),
lv_len TYPE i,
lv_hdl TYPE i.
DATA: BEGIN OF lt_result OCCURS 0,
line(100) TYPE c,
END OF lt_result.
DATA:lv_filename TYPE char128.
DATA oref TYPE REF TO cx_root.
DATA: BEGIN OF lt_data_txt OCCURS 0 ,
line(500),
END OF lt_data_txt.
DATA: BEGIN OF lt_data_binary OCCURS 0,
x(2000) TYPE x,
END OF lt_data_binary.
DATA: lv_binary_len TYPE i.
DATA: l_codepage(4) TYPE n .
DATA: l_encoding(20).
GET TIME.
* temp file name 用户名 日期 时间 传入的文件名
CONCATENATE sy-datum sy-uzeit INTO lv_filename.
* 将密码转化为SAP的格式
lv_len = strlen( c_pwd ).
CALL FUNCTION 'HTTP_SCRAMBLE'
EXPORTING
source = c_pwd
sourcelen = lv_len
key = c_key
IMPORTING
destination = lv_pwd. "加密密码
DATA: ls_ftpserver TYPE sapftp_servers.
SELECT SINGLE * FROM sapftp_servers INTO ls_ftpserver WHERE ftp_server_name = c_host AND ftp_server_port = 21.
IF sy-subrc <> 0.
ls_ftpserver-ftp_server_name = c_host.
ls_ftpserver-ftp_server_port = 21.
ls_ftpserver-description = '售后结算清单签章HTML文件服务'.
INSERT INTO sapftp_servers VALUES ls_ftpserver .
COMMIT WORK AND WAIT.
ENDIF.
TRY .
CALL FUNCTION 'FTP_CONNECT'
EXPORTING
user = c_user
password = lv_pwd
host = c_host
rfc_destination = c_rfcdest
IMPORTING
handle = lv_hdl.
CATCH cx_root INTO oref.
MESSAGE oref->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDTRY.
** 进入指定的FTP服务器目录
lv_command = 'cd /sh_settlement'.
TRY .
CALL FUNCTION 'FTP_COMMAND'
EXPORTING
handle = lv_hdl
command = lv_command
TABLES
data = lt_result.
LOOP AT lt_result.
WRITE:/ lt_result.
ENDLOOP.
CATCH cx_root INTO oref.
MESSAGE oref->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
PERFORM frm_disconnect.
RETURN.
ENDTRY.
APPEND '江' TO lt_data_txt.
APPEND 'a' TO lt_data_txt.
CALL FUNCTION 'SCP_CODEPAGE_BY_EXTERNAL_NAME'
EXPORTING
external_name = 'GB2312'
IMPORTING
sap_codepage = l_codepage.
l_encoding = l_codepage.
TRY.
**将文本内表转换为某种编码格式的二进制码流内表
CALL FUNCTION 'SCMS_TEXT_TO_BINARY'
EXPORTING
encoding = l_encoding
IMPORTING
output_length = lv_binary_len "编码后占多少字节
TABLES
text_tab = lt_data_txt
binary_tab = lt_data_binary.
CATCH cx_root INTO oref.
MESSAGE oref->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
RETURN.
ENDTRY.
TRY .
CALL FUNCTION 'FTP_R3_TO_SERVER'
EXPORTING
handle = lv_hdl
fname = lv_filename
blob_length = lv_binary_len
TABLES
blob = lt_data_binary. "二进制输出
CATCH cx_root INTO oref.
MESSAGE oref->get_text( ) TYPE 'S' DISPLAY LIKE 'E'.
PERFORM frm_disconnect.
RETURN.
ENDTRY.
FORM frm_disconnect.
* disconnect
* 关闭SAP与其他系统的连接
CALL FUNCTION 'FTP_DISCONNECT'
EXPORTING
handle = lv_hdl.
*关闭SAP与其他系统的RFC连接.
CALL FUNCTION 'RFC_CONNECTION_CLOSE'
EXPORTING
destination = c_rfcdest
EXCEPTIONS
OTHERS = 1.
ENDFORM.
20.4. 文件读写
DATA: file TYPE string VALUE `jzjflights.dat`,
wa TYPE spfli.
OPEN DATASET file FOR OUTPUT IN BINARY MODE.
SELECT *
FROM spfli
INTO wa.
TRANSFER wa TO file. "写
ENDSELECT.
CLOSE DATASET file.
DATA: file TYPE string VALUE `jzjflights.dat`,
wa TYPE spfli.
OPEN DATASET file FOR INPUT IN BINARY MODE.
DO.
"由于没有使用MAXIMUM LENGTH选项,所以每次读取的最大字节数由wa所占字节数决定
READ DATASET file INTO wa. "读
IF sy-subrc = 0.
WRITE: / wa-carrid,
wa-connid,
wa-countryfr,
wa-cityfrom,
wa-cityto,
wa-fltime,
wa-distance.
ELSE.
EXIT.
ENDIF.
ENDDO.
CLOSE DATASET file.